package com.begye.smartloan.apptest.homeadsource

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.daohang.DaoHangBottom
import com.begye.smartloan.apptest.databinding.AtHomeAdSourceBinding
import com.begye.smartloan.apptest.ext.toThousandUnitFormat
import com.begye.smartloan.apptest.fangkuanzhong.FangKuanZhongActivity
import com.begye.smartloan.apptest.gongong.YongHuLiuChengViewModel
import com.begye.smartloan.apptest.huankuan.HuanKuanActivity
import com.begye.smartloan.apptest.huankuan.HuanKuanViewModel
import com.begye.smartloan.apptest.jujue.JuJueActivity
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.outafter.OutRecordAfterActivity
import com.begye.smartloan.apptest.renzheng.RenZhengActivity
import com.begye.smartloan.apptest.renzheng.bangka.RenZhengBangKaActivity
import com.begye.smartloan.apptest.renzheng.document.RenZhengDocumentActivity
import com.begye.smartloan.apptest.renzheng.lianxiren.RenZhengLianXiRenActivity
import com.begye.smartloan.apptest.renzheng.selfie.RenZhengSelfieActivity
import com.begye.smartloan.apptest.shenhezhong.ShenHeZhongActivity
import com.begye.smartloan.apptest.tool.LoginTool
import com.begye.smartloan.apptest.tool.PageTagTool
import com.begye.smartloan.apptest.tool.SysConfigTool
import com.begye.smartloan.apptest.tool.addTouch
import com.data.datalib.ASSycManager
import com.util.utillib.extend.IntentFlagMode
import com.util.utillib.extend.toNextActivity
import com.vmadalin.easypermissions.EasyPermissions


@SuppressLint("CustomWelcomeScreen")
class HomeAdSourceHuanKuanActivity : AppCompatActivity() {

    private lateinit var _binding: AtHomeAdSourceBinding

    private val _homeAdSourceViewModel by lazy {
        ViewModelProvider(this)[HomeAdSourceViewModel::class.java]
    }

    private val _huanKuanViewModel by lazy {
        ViewModelProvider(this)[HuanKuanViewModel::class.java]
    }

    private val _yongHuLiuChengViewModel by lazy {
        ViewModelProvider(this)[YongHuLiuChengViewModel::class.java]
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtHomeAdSourceBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configYongHuLiuChengState()
        configUIState()
        configUIEvent()
        configUIData()
    }

    private fun configYongHuLiuChengState() {

        _homeAdSourceViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuLiuChengViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuLiuChengViewModel.onHasOrderStatusLiveData.observe(this) {
            it?.let { result ->
                when (result) {
                    PageTagTool.dai_huan_kuan -> {
                        toNextActivity(
                            activity = HuanKuanActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.fang_kuan_zhong -> {
                        toNextActivity(
                            activity = FangKuanZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.shen_he_zhone -> {
                        toNextActivity(
                            activity = ShenHeZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.ju_jue -> {
                        toNextActivity(
                            activity = JuJueActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }
                }
            }
        }

        _yongHuLiuChengViewModel.onDoingRenZhengPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingLianXiRenPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengLianXiRenActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingBangKaFrPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengBangKaActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingDocumentPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengDocumentActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingSelfiePageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengSelfieActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onAllDoneLiveData.observe(this) {
            if (it.isNotBlank()) {
                // to shen qing ye
                toNextActivity(
                    activity = HomeAdSourceActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }
    }

    private fun configUIState() {

        _huanKuanViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _huanKuanViewModel.onHuanKuanDetailLiveData.observe(this) {
            it?.let { result ->

                try {
                    _binding.atHomeAdSourceTopStepResultUtilisedValue.text =
                        result.optString("repayAmount", "0").toThousandUnitFormat()

                    _binding.atHomeAdSourceTopStepResultNextDayValue.text =
                        result.optString("repayDate", "")

                } catch (e: Exception) {
                    //
                }
            }
        }

        _homeAdSourceViewModel.onHomeDetailLiveData.observe(this) {
            it?.let { result ->
                _binding.atHomeAdSourceTopResultValue.text = result.optString("amount", "")
                _binding.atHomeAdSourceTopResultValue.text = result.optString("amount", "")

                _huanKuanViewModel.requestHuanKuanList()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun configUIEvent() {

        _binding.atHomeAdSourceStart.text = getString(R.string.huan_kuan_text_38)

        _binding.atHomeAdSourceNav.initConfig(
            leftResId = R.drawable.dres_6,
            rightResId = R.drawable.dres_4
        ) {
            if (0 == it) {
                _binding.atHomeAdSourceCouTiDrawer.refresh(this@HomeAdSourceHuanKuanActivity) {
                    if (_binding.atHomeAdSourceRootLayout.isDrawerOpen(_binding.atHomeAdSourceCouTiDrawer)) {
                        _binding.atHomeAdSourceRootLayout.closeDrawer(_binding.atHomeAdSourceCouTiDrawer)
                    }
                }

                if (_binding.atHomeAdSourceRootLayout.isDrawerOpen(_binding.atHomeAdSourceCouTiDrawer)) {
                    _binding.atHomeAdSourceRootLayout.closeDrawer(_binding.atHomeAdSourceCouTiDrawer)
                } else {
                    _binding.atHomeAdSourceRootLayout.openDrawer(_binding.atHomeAdSourceCouTiDrawer)
                }
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        _binding.atHomeAdSourceBottom.initConfig {
            if (it == DaoHangBottom.TYPE_HUAN_KUAN) {
                toNextActivity(
                    activity = HuanKuanActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _binding.atHomeAdSourceRecords.setOnClickListener {
            toNextActivity(
                activity = OutRecordAfterActivity::class.java,
                mode = IntentFlagMode.NEW_TASK
            )
        }

        _binding.atHomeAdSourceStart.setOnClickListener {
            if (checkPermission()) {
                try {
                    _yongHuLiuChengViewModel.requestYongHuLiuCheng()

                    ASSycManager.syncData(LoginTool.filterSession()) {}

                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            //
            addTouch(type = "home_ad_source_huan_kuan_commit_click")
        }
    }

    private fun configUIData() {
        _homeAdSourceViewModel.requestHomeDetail()
    }

    private fun checkPermission(): Boolean {
        val list = SysConfigTool.filterShouYeQuanXiang()
        return EasyPermissions.hasPermissions(
            this,
            *(list.toTypedArray<String>())
        )
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}

